package com.xt.favorites.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xt.favorites.admin.entity.UserEntity;
import com.xt.favorites.admin.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.common.utils.PageUtils;
import com.xt.common.utils.Query;

import com.xt.favorites.admin.dao.CollectDao;
import com.xt.favorites.admin.entity.CollectEntity;
import com.xt.favorites.admin.service.CollectService;

import javax.annotation.Resource;


@Service("collectService")
public class CollectServiceImpl extends ServiceImpl<CollectDao, CollectEntity> implements CollectService {

    @Resource
    CollectDao collectDao;
    @Resource
    UserService userService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<CollectEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("id",key).or().like("description",key).or().like("remark",key)
                    .or().like("title",key);
        }
        IPage<CollectEntity> page = this.page(
                new Query<CollectEntity>().getPage(params),
               wrapper
        );
        //根据用户id获取用户姓名
//        page.getRecords().stream().map(collectEntity -> {
//            Long userId = collectEntity.getUserId();
//            UserEntity byId = userService.getById(userId);
//            collectEntity.setUserName(byId.getUserName());
//            return collectEntity;
//        });
        for (CollectEntity collectEntity : page.getRecords()) {
            Long userId = collectEntity.getUserId();
            UserEntity byId = userService.getById(userId);
            collectEntity.setUserName(byId.getUserName());
        }

        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Integer>> console() {
        List<Map<String,Integer>> result = new ArrayList<Map<String, Integer>>();
        Map<String, Integer> collectAll = collectDao.getCollectAll();
        Map<String, Integer> collectTroday = collectDao.getCollectToday();
        result.add(collectAll);
        result.add(collectTroday);

        return result;

    }

}